package com.geek.jdbc.trans.customers;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
import com.geek.jdbc.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class QueryCustomerByName {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();

        String sql = "SELECT * FROM customers WHERE name =?";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String email = rs.getString("email");
                java.sql.Date birthday = rs.getDate("birthday");
                System.out.println("------------------------------");
                System.out.println("编号\t姓名\t邮箱\t生日");
                System.out.println(id + "\t" + name + "\t" + email + "\t" + birthday);
            } else {
                System.out.println("查无此人");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
